package com.etime.shycourse.controller.wx.manage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.orde.dto.SysOrderDetailsDto;
import com.etime.shycourse.orde.dto.stateNumDto;
import com.etime.shycourse.orde.pojo.Orders;
import com.etime.shycourse.orde.services.interfaces.OrderdetailServices;
import com.etime.shycourse.orde.services.interfaces.WxOrdersServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wb
 * @since 2022-01-10
 */
@Api(tags = "订单管理数据接口")
@RestController
@RequestMapping("/sys/orders")
public class WxSysOrdersController {
    @Autowired
    WxOrdersServices services;
    @Autowired
    OrderdetailServices orderdetailServices;

    @ApiOperation("后台 查询订单的状态的数量")
    @GetMapping("/state/get")
    public PojoMessage<stateNumDto> selectOrdersStateNumDto() {
        stateNumDto data = services.selectStateNumDto ();
        PojoMessage<stateNumDto> msg = new PojoMessage<> ();
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有指定教师的数据");
        }
        return msg;

    }

    @ApiOperation("后台 订单详情查询数据 可选填条件订单号 用户id 下单时间 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderno", value = "orderno 可选填条件订单号"),
            @ApiImplicitParam(name = "orderuserid", value = "orderuserid  可选填选择条件用户id"),
            @ApiImplicitParam(name = "orderstate", value = "orderstate  订单的状态"),
            @ApiImplicitParam(name = "orderpayment", value = "支付方式"),
            @ApiImplicitParam(name = "ordercreatetimeOne", value = "ordercreatetimeOne  可选填选择条件用户下单时间 开始"),
            @ApiImplicitParam(name = "ordercreatetimeTwo", value = "ordercreatetimeTwo  可选填选择条件用户下单时间 结束"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
    })
    @GetMapping("/page")
    @ResponseBody
    public PageMessage<SysOrderDetailsDto> selectOrderDetailsPageBy(Orders orders, Long pagenum, Long pagesize) {
        PageMessage<SysOrderDetailsDto> msg = new PageMessage<> ();
        Page<SysOrderDetailsDto> data = orderdetailServices.selectOrderDetailsListPageBys (orders, pagenum, pagesize);
        if (data != null && data.getSize () > 0) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }

    /**
     * 关闭订单必须要填写关闭订单的原因。如果没有填写，则不能关闭。
     * 关闭订单需要修改orders表的如下字段内容：
     * 前端写入 关闭交易描述	orderclosedesc =输入内容
     * <p>
     * 后台添加 交易关闭时间	orderclosetime	=当前时间
     * 后台添加 关闭交易用户id	ordercloseuserid =登录用户id
     * 后台添加 关闭交易用户姓名orderclosename =登录用户姓名
     * 后台添加 订单状态	orderstate = 3
     * <p>
     * upDataOrderById(String token, Orders order)
     *
     * @param order 根据订单id 进行修改
     * @return int
     */
    @ApiOperation("关闭订单 根据订单id")
    @PostMapping("/upDataOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderclosedesc", value = "必传 关闭交易描述"),
            @ApiImplicitParam(name = "orderstate", value = "订单状态 没有传 则后台添加 orderstate = 3"),
            @ApiImplicitParam(name = "其他数据后台根据token 添加"),
    })
    public BaseMessage upDataOrderById(Orders order, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        int rtn = services.upDataOrderById (token, order);
        if (rtn != 0) {
            msg.setCode (rtn);
            msg.setMsg ("关闭订单成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("关闭订单失败");
        }
        return msg;
    }
}

